from flask import Blueprint, render_template, request, redirect, url_for
from flask_login import login_user, logout_user, login_required
from sqlalchemy import or_

from forms.front import RegisterForm, LoginForm
from models.admin import Admin
from models.base import db
from models.category import Category
from models.item import Item
from models.user import User
from utils import restful

admin = Blueprint('admin', __name__, url_prefix='/admin')


@admin.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('admin/login.html')
    form = LoginForm(request.form)
    if form.validate():
        username = form.username.data
        password = form.password.data
        admin = Admin.query.filter_by(username=username).first()
        if not (admin and admin.check_password(password)):
            return restful.param_error('管理员用户不存在或密码错误')
        login_user(admin)
        return restful.success()
    else:
        print(restful.form_error(form))
        return restful.form_error(form)


@admin.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('admin.login'))


@admin.route('/')
# @login_required
def index():
    cates = Category.query.order_by(Category.create_time.desc()).all()
    return render_template('admin/index.html', cates=cates)


@admin.route('/modcate', methods=['POST'])
def mod_cate():
    cid = request.form.get('cid')
    name = request.form.get('name')
    url = request.form.get('url')
    cate = Category.query.get(cid)
    if Category.query.filter(or_(Category.name == name, Category.url == url)).count() == 2:
        return restful.param_error('名称或链接已存在')
    with db.auto_commit():
        cate.name = name
        cate.url = url
    return restful.success()


@admin.route('/addcate', methods=['POST'])
def add_cate():
    name = request.form.get('name')
    url = request.form.get('url')
    if Category.query.filter((Category.name == name)).first():
        return restful.param_error('该考试种类已存在')
    cate = Category()
    with db.auto_commit():
        cate.name = name
        cate.url = url
        db.session.add(cate)
    return restful.success()


@admin.route('/delcate')
def del_cate():
    cid = request.args.get('cid')
    cate = Category.query.get(cid)
    if not cate:
        return restful.param_error('该种类不存在')
    with db.auto_commit():
        db.session.delete(cate)
    return restful.success()


@admin.route('/item')
def item():
    items = Item.query.order_by(Item.create_time.desc()).all()
    return render_template('admin/item.html', items=items)


@admin.route('/moditem', methods=['POST'])
def mod_item():
    iid = request.form.get('iid')
    name = request.form.get('name')
    url = request.form.get('url')
    cate_name = request.form.get('cate')
    cate = Category.query.filter(Category.name == cate_name).first()
    if not cate:
        return restful.param_error('种类不存在')
    item = Item.query.get(iid)
    if Item.query.filter(or_(Item.name == name, Item.url == url)).count() == 2:
        return restful.param_error('名称或链接已存在')
    with db.auto_commit():
        item.name = name
        item.url = url
        item.category = cate
    return restful.success()


@admin.route('/additem', methods=['POST'])
def add_item():
    name = request.form.get('name')
    url = request.form.get('url')
    cate_name = request.form.get('cate')
    cate = Category.query.filter(Category.name == cate_name).first()
    if not cate:
        return restful.param_error('种类不存在')
    if Item.query.filter((Item.name == name)).first():
        return restful.param_error('该考试科目已存在')
    item = Item()
    with db.auto_commit():
        item.name = name
        item.url = url
        item.category = cate
        db.session.add(item)
    return restful.success()


@admin.route('/delitem')
def del_item():
    iid = request.args.get('iid')
    item = Item.query.get(iid)
    if not item:
        return restful.param_error('该科目不存在')
    with db.auto_commit():
        db.session.delete(item)
    return restful.success()


@admin.route('/fuser')
def show_fuser():
    users = User.query.order_by(User.create_time.desc()).all()
    return render_template('admin/fuser.html', users=users)


@admin.route('/moduser', methods=['POST'])
def mod_user():
    uid = request.form.get('uid')
    name = request.form.get('name')
    user = User.query.get(uid)
    if User.query.filter_by(username=name).first():
        return restful.param_error('该用户名已存在')
    with db.auto_commit():
        user.username = name
    return restful.success()


@admin.route('/adduser', methods=['POST'])
def add_user():
    name = request.form.get('name')
    if User.query.filter_by(username=name).first():
        return restful.param_error('该用户名已存在')
    user = User()
    with db.auto_commit():
        user.username = name
        user.password = '111111'
        db.session.add(user)
    return restful.success()


@admin.route('/deluser')
def del_user():
    uid = request.args.get('uid')
    user = User.query.get(uid)
    if not user:
        return restful.param_error('该用户不存在')
    with db.auto_commit():
        db.session.delete(user)
    return restful.success()


@admin.route('/buser')
def show_buser():
    admins = Admin.query.order_by(Admin.create_time.desc()).all()
    return render_template('admin/buser.html', admins=admins)


@admin.route('/modbuser', methods=['POST'])
def mod_buser():
    uid = request.form.get('uid')
    name = request.form.get('name')
    password = request.form.get('password')
    admin = Admin.query.get(uid)
    if Admin.query.filter_by(username=name).first():
        return restful.param_error('该用户名已存在')
    with db.auto_commit():
        admin.username = name
        admin.password = password
    return restful.success()


@admin.route('/addbuser', methods=['POST'])
def add_buser():
    name = request.form.get('name')
    password = request.form.get('password')
    if Admin.query.filter_by(username=name).first():
        return restful.param_error('该用户名已存在')
    admin = Admin()
    with db.auto_commit():
        admin.username = name
        admin.password = password
        db.session.add(admin)
    return restful.success()


@admin.route('/delbuser')
def del_buser():
    uid = request.args.get('uid')
    admin = Admin.query.get(uid)
    if not admin:
        return restful.param_error('该管理员不存在')
    with db.auto_commit():
        db.session.delete(admin)
    return restful.success()

